package com.ciaojian.client.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ciaojian.core.mapper.CouponMapper;
import com.ciaojian.core.mapper.DistUserMapper;
import com.ciaojian.core.model.*;
import com.ciaojian.core.model.vo.DistUserVo;
import com.ciaojian.core.model.vo.UserDistributorChooseVo;
import com.ciaojian.core.service.DistUserService;
import io.swagger.models.auth.In;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author oxiny
 */
@Service
public class DistUserServiceImpl extends DistUserService<DistUserMapper, DistUser> {
    @Resource
    private CouponMapper couponMapper;
    @Resource
    private CouponCodeServiceImpl couponCodeService;
    @Resource
    private DistGroupServiceImpl distGroupService;
    @Resource
    private OrderServiceImpl orderService;
    @Resource
    private DistRecordServiceImpl distRecordService;
    @Resource
    private StoreServiceImpl storeService;

    /**
     * 获取分销员客数信息
     *
     * @param distId 分销员
     * @param page   分页插件
     * @return /
     */
    public Page<Map<String, Object>> getCustomerByDistId(Integer distId, Page<Map<String, Object>> page) {
        return this.distRecordService.getBaseMapper().getCustomerByDistId(page, distId);
    }

    /**
     * 获取佣金明细
     *
     * @param distId 分销员
     * @return /
     */
    public Page<Map<String, Object>> getDistRecord(Integer distId, Page<Map<String, Object>> page) {
        return distRecordService.getBaseMapper().getDistRecordByDistId(page, distId);
    }

    /**
     * 获取分销员客数首次消费数量
     *
     * @param distId 分销员
    @return /
     */
    public Integer getOrderRecord(Integer distId) {
        return distRecordService.getBaseMapper().getOrderRecord(distId);
    }


    /**
     * 分销员数据
     *
     * @param userId 分销员
     * @return /
     */
    public Map<String, Object> data(Integer userId) {
        HashMap<String, Object> map = new HashMap<>(3);
        map.put("totalMoney", distRecordService.getBaseMapper().sumMoneyByDistId(userId));
        map.put("customerNum", distRecordService.getBaseMapper().countSuccessByDistId(userId));
        map.put("orderNum", distRecordService.getBaseMapper().countSubOrderNumByDistId(userId));
        return map;
    }

    /**
     * 根据ID查找券数据和可用门店名称
     *
     * @param userId 用户ID
     * @return 券数据和可用门店名称
     */
    public List<Map<String, Object>> findCouponAndStoreAvailableById(Integer userId) {
        DistUserVo distUserVo = baseMapper.findByUserId(userId);
        Assert.notNull(distUserVo, "邀请人" + userId + "不是分销员");
        List<Map<String, Object>> list = new ArrayList<>();
        for (String couponId : distUserVo.getCouponId().split(",")) {
            Map<String, Object> map = new HashMap<>(3);
            Coupon coupon = couponMapper.selectById(couponId);

            List<Store> stores = storeService.lambdaQuery()
                    .in(Store::getId, Arrays.asList(Optional.ofNullable(coupon.getStoreList()).orElseGet(String::new).split(",")))
                    .select(Store::getTitle)
                    .list();

            List<String> titles = stores.stream().map(Store::getTitle).collect(Collectors.toList());

            map.put("title", titles);
            map.put("coupon", coupon);
            map.put("distUser", distUserVo);
            list.add(map);
        }
        return list;
    }

    /**
     * 被分享用户选择奖励
     *
     * @param params /
     */
    @Transactional(rollbackFor = Exception.class)
    public void choose(UserDistributorChooseVo params) {
        // 判断是否领取过分销员的分销券
        Assert.isTrue(distRecordService.countByTargetId(params.getTargetId()) == 0, "您已领取");
        // Assert.isTrue(orderService.countNumberOfCompleteByUserId(params.getTargetId()) == 0, "该券只限新用户领取");

        DistGroup distGroup = distGroupService.getById(baseMapper.findByUserId(params.getSourceId()).getDistGroupId());
        // 领取券
        // couponCodeService.takeCode(params.getTargetId(), params.getCouponId());
        // 添加分销记录
        new DistRecord().setDistId(params.getSourceId())
                .setTargetId(params.getTargetId())
                .insert();
    }
}
